< previous page page_225 next page >

Page 225
In other words, rather than using an explicit Unicode-to-ANSI conversion, this function cheats by using the Unicode-to-ANSI conversion that Visual Basic does automatically when passing strings to API functions.
The nr.lpRemoteName field can be set using the following code, given a dynamically allocated byte array named ResourceNameArray:
Call CreateAnsiArray(resourcename, ResourceNameArray)
   nr.lpRemoteName = VarPtr(ResourceNameArray(0))
0225-01.gif
Figure S22-1:
The buffer string addresses remain valid after the RtlMoveMemory 
function is called
The RtlMoveMemory function will now work without crashing. You can see this in Figure S22-1, which shows the contents of the buffer loaded by the WNetGetResourceInformation function. The buffer starts with a NETRESOURCE structure and is followed by the string information. The pointers in the structure contain the addresses of those strings in the buffer as indicated by the arrows. When you copy the first part of the buffer into a NETRESOURCE-type variable, the pointers still correctly point to the string data in the original buffer.
Now all that remains is to convert the string data into a form that is accessible by Visual Basic.
The following function can do the trick:
' Retrieve a VB string given a pointer to a null-terminated string
Private Function GetVBString(ByVal ptr As Long)
   Dim StringLength As Long
   Dim TempString As String
   If ptr = 0 Then Exit Function
   StringLength = lstrlen(ptr)
   TempString = String$(StringLength + 1, 0)
   Call lstrcpystring(TempString, ptr)
   GetVBString = Left$(TempString, StringLength)
End Function

 
< previous page page_225 next page >